// eslint.config.mjs
import globals from 'globals';
import js from '@eslint/js';

// ---------- 插件 ----------
import pluginVue from 'eslint-plugin-vue';
import tseslint from '@typescript-eslint/eslint-plugin';

// Prettier
import eslintConfigPrettier from 'eslint-config-prettier';
import eslintPluginPrettier from 'eslint-plugin-prettier';

// ---------- 解析器 ----------
import * as vueParser from 'vue-eslint-parser';
import * as tsParser from '@typescript-eslint/parser';

// ---------- 配置 ----------
export default [
  // 1️⃣ 忽略文件
  {
    ignores: [
      '**/dist/**',
      '**/public/**',
      '**/node_modules/**',
      '**/*.min.js',
      '**/*.config.*',
      '**/*.tsbuildinfo',
      '**/manifest.json',
    ],
  },

  // 2️⃣ JS 通用
  {
    ...js.configs.recommended,
    languageOptions: {
      globals: { ...globals.browser, ...globals.node, uni: 'readonly',BlobPart: 'readonly' },
    },
  },

  // 3️⃣ TypeScript
  {
    files: ['**/*.{ts,mts,cts}'],
    languageOptions: {
      parser: tsParser,
      parserOptions: {
        sourceType: 'module',
      },
    },
    plugins: {
      '@typescript-eslint': tseslint,
    },
    rules: {
      ...tseslint.configs.recommended.rules,
      '@typescript-eslint/no-explicit-any': 'off',
      '@typescript-eslint/no-empty-function': 'off',
      '@typescript-eslint/no-empty-object-type': 'off',
      '@typescript-eslint/no-namespace': 'off',
      '@typescript-eslint/consistent-type-imports': [
        'error',
        { disallowTypeAnnotations: false, fixStyle: 'inline-type-imports' },
      ],
    },
  },

  // 4️⃣ TypeScript 声明文件
  {
    files: ['**/*.d.ts'],
    rules: {
      '@typescript-eslint/no-unused-vars': 'off',
    },
  },

  // 5️⃣ Vue SFC
  {
    files: ['**/*.vue'],
    languageOptions: {
      parser: vueParser,
      parserOptions: {
        parser: tsParser,          // <script setup lang="ts"> 支持
        sourceType: 'module',
      },
    },
    plugins: {
      vue: pluginVue,
    },
    processor: pluginVue.processors['.vue'],
    rules: {
      ...pluginVue.configs['flat/recommended'].rules,
      'vue/no-v-html': 'off',
      'vue/require-default-prop': 'off',
      'vue/multi-word-component-names': 'off',
      'vue/html-self-closing': [
        'error',
        {
          html: { void: 'always', normal: 'always', component: 'always' },
          svg: 'always',
          math: 'always',
        },
      ],
    },
  },

  // 6️⃣ Prettier（必须放最后，覆盖格式化冲突）
  {
    plugins: {
      prettier: eslintPluginPrettier,
    },
    rules: {
      ...eslintConfigPrettier.rules,
      'prettier/prettier': [
        'error',
        { endOfLine: 'auto' },
      ],
    },
  },
];